#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.role.controller;

import ${package}.basic.module.role.service.RoleService;
import ${package}.common.bean.po.Role;
import ${package}.common.bean.vo.AddRoleVo;
import ${package}.common.bean.vo.DisableOrEnableRoleVo;
import ${package}.common.bean.vo.DistributionRole;
import ${package}.common.bean.vo.JsonResult;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.bean.vo.SubPermission;
import ${package}.common.bean.vo.UpdateRoleVo;
import ${package}.common.bean.vo.UserPermissionVo;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.utils.SnowFlakeUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Description: 无法补充
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("/yk_api/role/")
@Api(value = "角色")
public class RoleController {

    /**
     * 角色Service
     */
    @Autowired
    private RoleService roleService;
    private int codeNumber = 1;

    /**
     * Description:
     * <根据用户主键获取非当前用户下的角色>
     * @author wupanhua
     * @date 10:36 2019/8/7
     * @return JsonResult
     **/
    @GetMapping("allRoles")
    @ApiOperation(value = "根据用户主键获取非当前用户下的角色",notes = "根据用户主键获取非当前用户下的角色")
    public JsonResult<List<Role>> queryNotInRoleByUserKey(){

        List<Role> roles = roleService.findAllRoles();

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, roles);
    }

    /**
     * Description:
     * <根据用户主键获取当前用户角色>
     * @author wupanhua
     * @date 10:37 2019/8/7
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("inRole")
    @ApiOperation(value = "根据用户主键获取当前用户角色",notes = "根据用户主键获取当前用户角色")
    @ApiImplicitParam(name = "id",value = "用户主键",dataType = "Long",paramType = "query",required = true)
    public JsonResult<List<Role>> queryInRoleByUserKey(Long id){
        List<Role> roles = roleService.findRolesByUserKey(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, roles);
    }

    /**
     * Description:
     * <获取用户的所有权限>
     * @author 周婷婷
     * @date 10:37 2019/8/7
     * @return JsonResult
     **/
    @GetMapping("getPermissByUserId")
    @ApiOperation(value = "获取用户的所有权限",notes = "获取用户的所有权限")
    public JsonResult<List<UserPermissionVo>> getPermissionByUserId(){
        List<UserPermissionVo> permission = roleService.getPermissionByUserId();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,permission);
    }


    /**
     * Description:
     * <角色下发>
     * @author wupanhua
     * @date 10:37 2019/8/7
     * @param distributionRole 1
     * @return JsonResult
     **/
    @PostMapping("distribution")
    @ApiOperation(value = "角色下发",notes = "角色下发")
    @ApiImplicitParam(name = "distributionRole",value = "角色下发实体类",dataType = "DistributionRole",paramType = "body",required = true)
    public JsonResult<Boolean> distribution(@RequestBody @Valid DistributionRole distributionRole){
        roleService.distributionRole(distributionRole);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, Boolean.TRUE);
    }


    /**
     * Description:
     * <获取角色列表>
     * @author wupanhua
     * @date 10:38 2019/8/7
     * @param queryBean 1
     * @return JsonResult
     **/
    @PostMapping("roleList")
    @ApiOperation(value = "获取角色列表",notes = "获取角色列表")
    @ApiImplicitParam(name = "queryBean",value = "查询条件",dataType = "QueryBean",paramType = "body",required = true)
    public JsonResult<PageInfo<Role>> findRoleList(@RequestBody @Valid QueryBean queryBean){
        PageInfo<Role> roles = roleService.findRoleListLimit(queryBean);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, roles);
    }


    /**
     * Description:
     * <新增角色>
     * @author wupanhua
     * @date 10:38 2019/8/7
     * @param addRoleVo 1
     * @return JsonResult
     **/
    @PutMapping("addRole")
    @ApiOperation(value = "新增角色",notes = "新增角色")
    @ApiImplicitParam(name = "addRoleVo",value = "新增角色实体类",dataType = "AddRoleVo",paramType = "body",required = true)
    public JsonResult<Role> addRole(@RequestBody @Valid AddRoleVo addRoleVo){

        Role role = new Role();
        BeanUtils.copyProperties(addRoleVo, role);
        String roleName = addRoleVo.getName();
        //名字去重校验
        List<String> roleNameList = roleService.queryAllRoleName();
        if (roleNameList.contains(roleName)) {
            return JsonResult.getInstant(602,"角色名称重复");
        }
        role.setId(SnowFlakeUtil.getID());
        codeNumber++;
        role.setCode("R"+ codeNumber);
        roleService.addRole(role);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, role);
    }

    /**
     * Description:
     * <删除角色>
     * @author wupanhua
     * @date 10:39 2019/8/7
     * @param id 1
     * @return JsonResult
     **/
    @DeleteMapping("deleteRole/{id}")
    @ApiOperation(value = "删除角色",notes = "删除角色")
    @ApiImplicitParam(name = "id",value = "角色主键",dataType = "Long",paramType = "path",required = true)
    public JsonResult<Long> deleteRole(@PathVariable("id") Long id){
        roleService.deleteRoleByRoleKey(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, id);
    }

    /**
     * Description:
     * <下发权限>
     * @author wupanhua
     * @date 10:39 2019/8/7
     * @param subPermission 1
     * @return JsonResult
     **/
    @PutMapping("distributionPermission")
    @ApiOperation(value = "下发权限",notes = "下发权限")
    @ApiImplicitParam(name = "subPermission",value = "下发权限实体类",dataType = "SubPermission",paramType = "body",required = true)
    public JsonResult<Boolean> distributionPermission(@RequestBody @Valid SubPermission subPermission){
        roleService.distributionPermission(subPermission);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, Boolean.TRUE);
    }

    /**
     * Description:
     * <禁用/启用角色>
     * @author wupanhua
     * @date 10:39 2019/8/7
     * @param disableOrEnableRoleVo 1
     * @return JsonResult
     **/
    @PostMapping("disableOrEnableRole")
    @ApiOperation(value = "禁用/启用角色",notes = "禁用/启用角色")
    @ApiImplicitParam(name = "disableOrEnableRoleVo",value = "启用或禁用实体类",dataType = "DisableOrEnableRoleVo",paramType = "body",required = true)
    public JsonResult<Role> disableOrEnableRole(@RequestBody @Valid DisableOrEnableRoleVo disableOrEnableRoleVo){

        Role role = new Role();

        BeanUtils.copyProperties(disableOrEnableRoleVo,role);

        roleService.disableOrEnableRole(role);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, role);
    }

    /**
     * Description:
     * <根据id查询角色信息>
     * @author wupanhua
     * @date 10:40 2019/8/7
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("queryRoleById")
    @ApiOperation(value = "根据id查询角色信息",notes = "根据id查询角色信息")
    @ApiImplicitParam(name = "id",value = "角色主键",dataType = "Long",paramType = "query",required = true)
    public JsonResult<Role> queryRoleById(Long id){
        Role role = roleService.queryRoleById(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,role);
    }

    /**
     * Description:
     * <根据id更新角色信息>
     * @author wupanhua
     * @date 10:41 2019/8/7
     * @param updateRoleVo 1
     * @return JsonResult
     **/
    @PostMapping("updateRoleById")
    @ApiOperation(value = "根据id更新角色信息",notes = "根据id更新角色信息")
    @ApiImplicitParam(name = "updateRoleVo",value = "更新角色实体类",dataType = "UpdateRoleVo",paramType = "body",required = true)
    public JsonResult<Role> updateRoleById(@RequestBody @Valid UpdateRoleVo updateRoleVo){

        Role role = new Role();
        BeanUtils.copyProperties(updateRoleVo,role);

        roleService.updateRoleById(role);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, role);
    }

    /**
     * Description:
     * <批量删除角色>
     * @author wupanhua
     * @date 10:41 2019/8/7
     * @param keys 1
     * @return JsonResult
     **/
    @PostMapping("removeRolesBykeys")
    @ApiOperation(value = "批量删除角色",notes = "批量删除角色")
    @ApiImplicitParam(name = "keys",value = "角色主键列表",dataType = "Long",paramType = "body",required = true,allowMultiple = true)
    public JsonResult<List<Role>> removeRolesBykeys(@RequestBody List<Long> keys){
        List<Role> roles = roleService.removeRolesBykeys(keys);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, roles);
    }

    /**
     * Description:
     * <查询是否重复名称>
     * @author wupanhua
     * @date 10:42 2019/8/7
     * @param name 1
     * @param id 2
     * @return JsonResult
     **/
    @GetMapping("roleVerifyName")
    @ApiOperation(value = "查询是否重复名称",notes = "查询是否重复名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "角色名称",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "id",value = "角色主键",dataType = "Long",paramType = "query",required = true)})
    public JsonResult<Boolean> roleVerifyName(@RequestParam(value = "name") String name, @RequestParam(value = "id") Long id){
        Boolean flag = roleService.roleVerifyName(name, id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,flag);
    }

    /**
     * Description:
     * <查询编码是否重复>
     * @author wupanhua
     * @date 10:42 2019/8/7
     * @param code 1
     * @return JsonResult
     **/
    @GetMapping("roleVerifyCode")
    @ApiOperation(value = "查询编码是否重复",notes = "查询编码是否重复")
    @ApiImplicitParam(name = "code",value = "编码",dataType = "String",paramType = "query",required = true)
    public JsonResult<Boolean> roleVerifyCode(String code){
        Boolean flag = roleService.roleVerifyCode(code);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,flag);
    }
}
